﻿<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>数据状态查看分析</title>
    <script src="/webjars/miniui/boot.js" type="text/javascript"></script>
    <script src="/static/js/common/common.js" type="text/javascript"></script>
    <script type="text/javascript" src="/static/js/main-page/echarts.min.js"></script>
    <link href="/static/css/resource.css" rel="stylesheet" type="text/css" />
    <link href="/webjars/miniui/css/demo.css" rel="stylesheet" type="text/css" />
    <link href="/static/css/default.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="/static/css/main-page/g-reset.css" />
    <style type="text/css">
        body {
            margin:0;
            padding:0;
            border:0;
            width:100%;
            height:100%;
            overflow:hidden;
        }
        .discell {
            text-align:right;
            width:80px;
        }
        td .mini-grid-cell-inner {
            text-align:center;
        }
        .unitShow {
            display:none;
        }
    </style>
</head>
<body>
<div class="mini-fit" style="height:100%;width: 100%;overflow-x: auto;overflow-y: auto;" id="form1" name="form1">
    <div style="width:100%;height:100%;overflow-y:auto">
        <div id="div1" style="border-bottom: 1px solid #dfdfdf;">
            <table style="border-collapse:separate; border-spacing:10px;margin-left: auto;">
                <tbody>
                <tr>
                    <#if judgeManage == "true">
                        <td class="discell">省份:</td>
                        <td>
                            <input name="province" id="province" class="mini-combobox" style="width: 140px" showNullItem="true"
                                   textField="codeName" valueField="codeId" emptyText="请选择..." allowInput="true"
                                   url="/azbj/sjtjfx/xzzttj/getProvince" />
                        </td>
                    </#if>
                    <td class="discell">类别:</td>
                    <td>
                        <input name="sort" id="sort" class="mini-combobox" style="width: 160px" showNullItem="true"
                               textField="value" valueField="key" emptyText="请选择..."
                               url="/azbj/sjtjfx/xzzttj/getSort"
                               onValueChanged="clickSort()" />
                    </td>
                    <td class="discell unitShow">单位名称:</td>
                    <td>
                        <input name="unitInput" id="unitInput" class="mini-combobox unitShow" style="width: 125px" showNullItem="true"
                               textField="addusername" valueField="adduserid" emptyText="请选择..." allowInput="true" />
                    </td>
                    <td class="discell">时间范围:</td>
                    <td>
                        <input property="editor" class="mini-datepicker" id="begin" name="begin" allowInput="false"
                               emptyText="开始日期" style="width: 100%;" ondrawdate="onDrawDate" />
                    </td>
                    <td>
                        <input property="editor" class="mini-datepicker" id="end" name="end" allowInput="false"
                               emptyText="结束日期" style="width: 100%;" ondrawdate="onDrawDate" />
                    </td>
                    <td style="text-align:right;" colspan="5">
                        <a class="mini-button" onclick="statistics()">统计</a>
                        <a class="mini-button" onclick="reset()">重置</a>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <div class="mini-fit" style="overflow-y:auto">
            <div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 100%;">

            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    mini.parse();
    var begin = mini.get("begin"); // 开始时间
    var end = mini.get("end");    // 结束时间
    var sort = mini.get("sort");  // 类别
    var province;  //省份
    var unitInput = mini.get("unitInput");//单位
    var pageSize = 200;
    var sortDetailsArray = [];
    var sortDetails;
    var totalField = 0;


    //时间校验
    function onDrawDate(e) {
        var date = e.date;
        var d = new Date();
        var y = new Date();
        if (date.getTime() > d.getTime()) {
            e.allowSelect = false;
        }
        y.setFullYear(y.getFullYear() - 1)
        if (date.getTime() < y.getTime()) {
            e.allowSelect = false;
        }
    }

    //页面加载
    $(function () {
        setDate();
        getPstateAll();
    });

    //时间设置
    function setDate() {
        var y = new Date();
        y.setFullYear(y.getFullYear() - 1)
        begin.setValue(new Date(y.getTime() + 24 * 60 * 60 * 1000));
        end.setValue(new Date());
    }

    //点击按钮
    function statistics() {
        if (begin.getValue() > end.getValue()) {
            mini.alert("开始时间不能大于结束时间");
            return;
        }
        sortShow();
        //获取数据
        getData(0, pageSize);
        if (province != null) {
            //获取所有单位
            getAllUnit();
        }

    }

    //重置按钮
    function reset() {
        sort.setValue(null);
        province.setValue(null);
        unitInput.setData(null);
        sortShow();
        setDate();
        getPstateAll();
    }

    //获取当前省份
    function getCurrentProvince() {
        $.ajax({
            url: '/azbj/sjtjfx/xzzttj/getCurrentProvince',
            type: "GET",
            success: function (res) {
                province = {value: res};
            }
        });
    }

    //类别显隐
    function sortShow() {
        if (province.value != null && province.value != '') {
            $('.unitShow').show();
        } else {
            $('.unitShow').hide();
        }
    }

    //获取所有的新增状态
    function getPstateAll() {
        $.ajax({
            url: '/azbj/sjtjfx/xzzttj/getSortDetails',
            type: "GET",
            data: {id: ''},
            success: function (res) {
                sortDetailsArray = [];
                sortDetails = res;
                for (var i = 0; i < res.length; i++) {
                    sortDetailsArray.push(res[i].id)
                }
                if (${judgeManage}) {
                    province = mini.get("province");
                } else {
                    getCurrentProvince();
                }
                //获取数据
                getData(0, pageSize);
            }
        });
    }

    //获取数据
    function getData(pageIndex, pageSize) {
        mini.mask({
            el: document.body,
            cls: 'mini-mask-loading',
            html: '数据加载中…'
        });
        $.ajax({
            url: '/azbj/sjtjfx/xzzttj/getResultData',
            type: "GET",
            traditional: true,
            data: {
                pageIndex: pageIndex,
                pageSize: pageSize,
                begin: mini.formatDate(begin.getValue(), 'yyyy-MM-dd'),
                end: mini.formatDate(end.getValue(), 'yyyy-MM-dd'),
                pstateType: sortDetailsArray,
                unitId: unitInput.value,
                province: province.value == null || province.value === '' ? null : province.value
            },
            success: function (res) {
                mini.unmask();
                var resData = JSON.parse(res);
                createTable(resData.data, resData.total);
            },
            error: function () {
                mini.unmask();
            }
        });
    }

    //获取所有单位
    function getAllUnit() {
        $.ajax({
            url: '/azbj/sjtjfx/xzzttj/getUnit',
            type: "GET",
            traditional: true,
            data: {
                begin: mini.formatDate(begin.getValue(), 'yyyy-MM-dd'),
                end: mini.formatDate(end.getValue(), 'yyyy-MM-dd'),
                pstateType: sortDetailsArray,
                province: province.value == null || province.value === '' ? null : province.value
            },
            success: function (res) {
                var data = JSON.parse(res.data);
                unit = data.unit;
                unitInput.setData(unit);
            }
        });
    }

    //动态获取新增状态
    function getSortDetails(id) {
        sortDetailsArray = [];
        $.ajax({
            url: '/azbj/sjtjfx/xzzttj/getSortDetails',
            type: "GET",
            data: {id: id},
            success: function (res) {
                sortDetails = res;
                for (var i = 0; i < res.length; i++) {
                    sortDetailsArray.push(res[i].id)
                }
            }
        });
    }

    //生成表格
    function createTable(data, total) {
        var gridView = mini.get("datagrid1");
        if (total === 0) {
            gridView.set({
                columns: [
                    {field: 'name', header: '暂无数据', headerAlign: 'center', autoescape: true}
                ]
            });
            gridView.reload();
            mini.alert("查询暂无数据");
            return;
        }
        var columns = [
            {type: "indexcolumn"},
            {field: 'id', header: '编号', width: 70, align: 'left', visible: false},
            {field: 'name', header: '单位名称', width: 200, headerAlign: 'center', autoescape: true, renderer:'setUnitName'},
            {
                header: '数据来源统计',
                headerAlign: 'center',
                columns: [{field: 'name', header: '', width: 120, headerAlign: 'center', autoescape: true}]
            }
        ];
        if (sortDetails != null) {
            columns[3].columns.pop();
            for (var i = 0; i < sortDetails.length; i++) {
                columns[3].columns.push({
                    field: 'amount' + sortDetails[i].id,
                    sortField: 'amount' + sortDetails[i].id,
                    header: sortDetails[i].name,
                    width: 200,
                    headerAlign: 'center',
                    allowSort: true,
                    autoescape: true
                });
            }
        }
        gridView.set({
            url: '/azbj/sjtjfx/xzzttj/getResultData',
            ajaxOptions: {
                type: "GET",
                traditional: true,
                data: {
                    begin: mini.formatDate(begin.getValue(), 'yyyy-MM-dd'),
                    end: mini.formatDate(end.getValue(), 'yyyy-MM-dd'),
                    pstateType: sortDetailsArray,
                    unitId: unitInput.value,
                    province: province.value == null || province.value === '' ? null : province.value
                }
            },
            showPager: true,
            sizeList: [pageSize],
            pageSizeWidth: 55,
            pageSize: pageSize,
            totalCount: total,
            columns: columns,
            allowAlternating: true, //显示间行色
            multiSelect: true,
            fitColumns: false, //使用过滤行必须设定这个全充满，否则会有列对齐问题。
            allowCellEdit: true,
            allowCellSelect: true,
            showReloadButton: false,
            showFilterRow: false,
            showModified: false,
            allowSortColumn: true,
            DoubleBuffered: true,
            allowMoveColumn: false
        });
        gridView.setData(data);
        //gridView.render(document.getElementById("datagrid1"));
        gridView.render();
    }

    //获取新增状态
    function clickSort() {
        var id = sort.value;
        getSortDetails(id);
    }

    //设置名称
    function setUnitName(e){
        if(unitInput.getData().length>1){
            return e.value;
        }
        for(var item of province.data){
            if (e.value == item.codeId){
                return item.codeName;
            }
        }
        return '未知';
    }

</script>

</body>
</html>